package com.keepers.repair.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.keepers.repair.R;
import com.keepers.repair.helper.ConsValue;
import com.keepers.repair.helper.IntentHelper;
import com.keepers.repair.net.DataLoader;
import com.keepers.repair.net.LogUtil;
import com.keepers.repair.utils.AlertToast;
import com.keepers.repair.utils.CollectionUtil;
import com.keepers.repair.utils.DefaultUtils;
import com.keepers.repair.utils.DialogUtil;
import com.keepers.repair.utils.GlideEngine;
import com.keepers.repair.utils.UtilsKt;
import com.keepers.repair.view.TitleBar;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.io.FileNotFoundException;
import java.util.Date;
import java.util.List;

/**
 * auth aboom
 * date 2018/11/21
 */
public class SendActivity extends BaseActivity {

    public static final int imageRequestCode = 0x11;
    public static final int editRequestCode  = 0x12;
    public static final int vedioRequestCode = 0x13;
    public static final int voiceRequestCode = 0x14;
    private String         gid;
    private ProgressDialog progressDialog;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
        progressDialog = DialogUtil.getProgressDialog(this, "正在上传...");
        gid = getIntent().getStringExtra(ConsValue.INTENT_KEY);
        Log.e("aa", " gid = " + gid);
        TitleBar titleBar = findViewById(R.id.title_bar);
        titleBar.initializeHeader("汇报进度");
        f(R.id.main_nav_menu_item_camera).setOnClickListener(v -> {
            Matisse.from(this)
                    .choose(MimeType.ofImage())
                    .showSingleMediaType(true)
                    .capture(true)
                    .captureStrategy(new CaptureStrategy(true, "com.keepers.repair.fileprovider"))
                    .maxSelectable(1)
                    .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                    .thumbnailScale(0.85f)
                    .imageEngine(new GlideEngine())
                    .theme(R.style.Matisse_Dracula)
                    .forResult(imageRequestCode);
        });
        f(R.id.main_nav_menu_item_vedio).setOnClickListener(v -> {
            Intent intent = new Intent(this, MovieActivity.class);
            startActivityForResult(intent, vedioRequestCode);
        });
        f(R.id.main_nav_menu_item_voice).setOnClickListener(v -> {
            Intent intent = new Intent(this, RecordingActivity.class);
            startActivityForResult(intent, voiceRequestCode);
        });
        f(R.id.main_nav_menu_item_edit).setOnClickListener(v -> {
            Intent intent = IntentHelper.goEdit(this);
            startActivityForResult(intent, editRequestCode);
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case imageRequestCode:
                    List<Uri> mSelected = Matisse.obtainResult(data);
                    Log.d("aa", "mSelected: " + mSelected);
                    if (CollectionUtil.isNotEmpty(mSelected)) {
                        Uri uri = mSelected.get(0);
                        try {
                            Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
                            String s = UtilsKt.encodeBase64Image(Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() / 10, bitmap.getHeight() / 10, true));
                            uploadFile(3, s);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case vedioRequestCode:
                    String filePath = data.getStringExtra(ConsValue.INTENT_KEY);
                    String encodeBase64File = UtilsKt.encodeBase64File(filePath);
                    uploadFile(2, encodeBase64File);
                    break;
                case voiceRequestCode:
                    String voiceFilePath = data.getStringExtra(ConsValue.INTENT_KEY);
                    String str = UtilsKt.encodeBase64File(voiceFilePath);
                    uploadFile(1, str);
                    break;
                case editRequestCode:
                    String edit = data.getStringExtra("edit");
                    LogUtil.e("edit: " + edit);
                    if (!progressDialog.isShowing()) {
                        progressDialog.show();
                    }
                    rxHelper.pend(DataLoader.uploadOverhaulResult(gid, edit, aBoolean -> {
                        progressDialog.cancel();
                        AlertToast.show(DefaultUtils.value(aBoolean) ? "上传成功" : "上传失败");
                    }));
                default:
                    break;
            }
        }
    }

    private void uploadFile(int type, String Base64Str) {
        if (!progressDialog.isShowing()) {
            progressDialog.show();
        }
        rxHelper.pend(DataLoader.repairUploadFiles(String.valueOf(new Date().getTime()), type, gid, Base64Str, aBoolean -> {
            progressDialog.cancel();
            AlertToast.show(DefaultUtils.value(aBoolean) ? "上传成功" : "上传失败");
        }));
    }
}
